//
//  XDProductsIntroduceController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/9.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDProductsIntroduceController.h"
#import "XDMyInvestmentModel.h"
#import "XDProductsIntroduceView.h"
#import "AFHTTPSessionManager+POST.h"
#import "MJExtension.h"
#import "XDBackMoneyViewController.h"
#import "XDProductsIntroduceCell.h"
@interface XDProductsIntroduceController ()<UITableViewDelegate, UITableViewDataSource, XDProductsIntroduceCellDelegate>
@property (nonatomic, strong) NSArray *desArr;

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, weak) XDProductsIntroduceView *headerView;
@property (nonatomic, strong)XDMyInvestmentModel *model;
@property (nonatomic, weak) UITableViewCell *cell;

@property (nonatomic, assign) BOOL isRotation;

@property (nonatomic, weak) UIView *cellView;

@property (nonatomic, weak) UIImageView *img;
@end

@implementation XDProductsIntroduceController

- (NSArray *)desArr {
    if (_desArr == nil) {
        _desArr = @[@"下个还款日",@"项目概况",@"购买合同"];
    }
    return _desArr;
}
- (void)loadData {
    
    NSDictionary * param = @{@"method":@"investmentInfo",@"investId":[NSString stringWithFormat:@"%zd",self.investId]};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            /*
             code = 100000;
             message = "\U64cd\U4f5c\U6210\U529f";
             result =     {
             inMoney = 111;
             inTime = "2017-01-16 14:41:19";
             interestType = "\U81ea\U6295\U8d44\U65e5\U6b21\U65e5\U8d77\Uff0c\U6309\U65e5\U8ba1\U606f";
             investId = 23;
             isMulti = 0;
             limitTime = 1;
             limitType = "\U6708";
             name = "\U4f18\U9009\U8ba1\U5212-1\U4e2a\U6708";
             nearBackTime = "2017-02-17 14:41:19";
             payBackTime = "2017-02-17 14:41:19";
             paybackType = 2;
             postId = 27;
             postNumber = QY170004;
             products = "\U4f18\U9009\U8ba1\U5212\U662f\U7279\U5b9a\U4f18\U8d28\U6807\U7684\U3001\U7b80\U5316\U7528\U6237\U6311\U9009\U3001\U63d0\U4f9b\U7a33\U5b9a\U6536\U76ca\U7684\U667a\U80fd\U6295\U6807\U5de5\U5177\U3002\U901a\U8fc7\U4f18\U9009\U8ba1\U5212\Uff0c\U53ef\U4ee5\U5206\U6563\U6295\U6807\Uff0c\U964d\U4f4e\U6295\U8d44\U98ce\U9669\Uff0c\U6bcf\U671f\U56de\U6b3e\U8d44\U91d1\U5c06\U8fdb\U884c\U5faa\U73af\U590d\U6295\Uff0c\U6700\U5927\U5316\U7528\U6237\U7684\U8d44\U91d1\U4f7f\U7528\U6548\U7387\Uff0c\U6ee1\U8db3\U7528\U6237\U591a\U6837\U5316\U7684\U7406\U8d22\U9700\U6c42\U3002";
             safeguardWay = "\U8d44\U91d1\U6258\U7ba1+\U62b5\U62bc\U7269\U6258\U7ba1";
             yearRate = "7.5";
             };
             type = success;
             */
            
            self.model = [XDMyInvestmentModel mj_objectWithKeyValues:responseObject[@"result"]];
            self.headerView.model = self.model;
            [self.tableView reloadData];
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
            
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUI];
    [self loadData];
}

- (void)setUI {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:kScreenB style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:tableView];
    XDProductsIntroduceView *view = [[NSBundle mainBundle] loadNibNamed:@"XDProductsIntroduceView" owner:self options:nil].firstObject;
    view.backgroundColor = kColorRGB(239, 232, 210);
    //view.model = self.model;
    self.headerView = view;
    tableView.tableHeaderView = view;
    self.tableView = tableView;
}

- (void)navigationBarCof {
    
    self.title = @"项目介绍";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTintColor:kColorRGB(51, 51, 51)];
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:kColorRGB(51, 51, 51),NSFontAttributeName:kBaseFont_(19)}];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self navigationBarCof];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}


- (void)back {
    [self.navigationController.navigationBar setTintColor:kColorRGB(187,147,115)];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *myID = @"MyInvestmentID";
    XDProductsIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:myID];
    if (cell == nil) {
        cell = [[XDProductsIntroduceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myID];
    }
    cell.isVisible = self.isRotation;
    cell.tag = indexPath.section;
    if (indexPath.section == 0) {
        [cell productsIntroduceCellWithTitle:self.desArr[indexPath.section] nearBackTimeLb:self.model.nearBackTime products:self.model.products];
    } else {
         [cell productsIntroduceCellWithTitle:self.desArr[indexPath.section] nearBackTimeLb:nil products:self.model.products];
    }
    cell.delegate = self;
   
    return cell;

}

- (UILabel *)lableWithFrame:(CGRect)rect textAlignment:(NSTextAlignment)textAlignment textColor:(UIColor *)textColor font: (UIFont *)font text: (NSString *)text {
    UILabel *desLb = [[UILabel alloc] initWithFrame:rect];
    desLb.textAlignment = textAlignment;
    desLb.textColor = textColor;
    desLb.font = font;
    desLb.text = text;
    return desLb;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isRotation && indexPath.section == 1) {
        return 140;
    }
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 30;
    }
    return 0.0001;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    if (offset.y <= 0) {
        offset.y = 0;
    }
    
    scrollView.contentOffset = offset;
}


- (void)productsCellBtnClick:(XDProductsIntroduceCell *)cell {
    
    if (cell.tag == 1) {
        self.isRotation = !self.isRotation;
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:cell.tag];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
        
    } else if (cell.tag == 0) {
        
        //        investId
         XDBackMoneyViewController *back = [[XDBackMoneyViewController alloc] init];
         back.investId = self.model.investId;
         [self.navigationController pushViewController:back animated:YES];
        
        
    } else if (cell.tag == 2) {
        
        
    }
    
}
- (void)productsLinkClick:(XDProductsIntroduceCell *)cell {
    
    NSLog(@"查看原标");
    
}
@end
